﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfFunctionTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        CmdFunction cmdFunction = new CmdFunction();
        public MainWindow()
        {
            InitializeComponent();
            cmdFunction.CmdOutputEvent += CmdFunction_CmdOutputEvent; ;
            cmdFunction.CmdErrorEvent += CmdFunction_CmdErrorEvent;
        }

        private void CmdFunction_CmdOutputEvent(string output)
        {
            if (!string.IsNullOrEmpty(output))
            {
                // 处理输出数据，例如将其添加到UI控件中
                Dispatcher.Invoke(() => {
                    CmdOutputTextBox.AppendText(output + Environment.NewLine);
                    CmdOutputTextBox.ScrollToEnd(); // 将滚动条滚动到最底部
                });
            }
            Console.WriteLine("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.......");
        }

        private void CmdFunction_CmdErrorEvent(string output)
        {
            if (!string.IsNullOrEmpty(output))
            {
                // 处理错误数据，例如将其添加到UI控件中
                Dispatcher.Invoke(() => CmdOutputTextBox.AppendText("Error: " + output + Environment.NewLine));
            }
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            if (StartCmdPanel.Visibility == Visibility.Visible)
            {
                return;
            }
            else
            {
                StartCmdPanel.Visibility = Visibility.Visible;
            }

            cmdFunction.StartCmdProcess();
        }
        private void StartCmdStop_Click(object sender, RoutedEventArgs e)
        {
            cmdFunction.ExitCmd();
            StartCmdPanel.Visibility = Visibility.Collapsed;
        }

        private void StartCmdSend_Click(object sender, RoutedEventArgs e)
        {
            cmdFunction.ExecuteCommand(CmdCommandInput.Text);
        }


        private void CmdCommandInput_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmdFunction.ExecuteCommand(CmdCommandInput.Text);
                CmdCommandInput.Text = "";
            }
        }
    }
}
